﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinForm {
    public partial class FormGroupList : Form
    {
        BLL.ContactGroup group = new BLL.ContactGroup();
        public FormGroupList()
        {
            InitializeComponent();
        }

        private void FormGroupList_Load(object sender, EventArgs e)
        {
            fill();
        }

        public void fill()
        {
            dgvGroupList.DataSource = group.GetList("");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormGroupAdd f = new FormGroupAdd();
            f.ShowDialog();
            fill();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvGroupList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = 0;
            try
            {
                n = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }
            catch (Exception)
            {
                MessageBox.Show("请选择有效的行记录。");
                return;
            }
            FormGroupDetail f = new FormGroupDetail(n);
            f.ShowDialog();
            fill();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }
            catch(Exception)
            {
                MessageBox.Show("请选择有效的记录。");
                return;
            }
            DialogResult dgr = MessageBox.Show("确定要删除该记录吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dgr == DialogResult.Yes)
            {
                if (group.Delete(id)) {
                    MessageBox.Show("成功删除指定分类。", "SFROST", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else {
                    MessageBox.Show("该分类下已存在联系人，不允许删除！");
                }                
            }
            fill();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int n = 0;
            try
            {
                n = (int)dgvGroupList.CurrentRow.Cells[0].Value;
            }catch(Exception)
            {
                MessageBox.Show("请选择有效的行记录。");
                return;
            }
            FormGroupDetail f = new FormGroupDetail(n);
            f.ShowDialog();
            fill();
        }
    }
}
